package com.incar.chuxing.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.incar.chuxing.R;

/**
 * Created by user1 on 2017/7/25.
 */

public class NumberButton extends LinearLayout implements View.OnClickListener, TextWatcher, View.OnFocusChangeListener, TextView.OnEditorActionListener {

    private int mAvaiValue = Integer.MAX_VALUE;     //可用数量
    private int mMaxValue = Integer.MAX_VALUE;     //最大数量，默认无限制
    private int chooseCount;
    private int chooseCountPre;
    private EditText mChooseCount;
    private OnCountChangeListener mOnCountChangeListener;
    private Button addButton;
    private Button subButton;
    private Context mContext;


    public NumberButton(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public NumberButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public NumberButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.layout_number_button, this);
        addButton = (Button) findViewById(R.id.button_add);
        addButton.setOnClickListener(this);
        subButton = (Button) findViewById(R.id.button_sub);
        subButton.setOnClickListener(this);
        mChooseCount = ((EditText) findViewById(R.id.text_count));
        mChooseCount.addTextChangedListener(this);
        mChooseCount.setOnClickListener(this);
        mChooseCount.setOnEditorActionListener(this);
        mChooseCount.setOnFocusChangeListener(this);
        if (!mChooseCount.getText().toString().trim().isEmpty()) {
            chooseCount = Integer.parseInt(mChooseCount.getText().toString().trim());
        }
    }


    @Override

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_sub) {
            if (mChooseCount.isFocused()) {
                chooseCount = chooseCountPre;
            }
            mChooseCount.clearFocus();
            if (chooseCount > 1) {
                //正常减
                mChooseCount.setText(--chooseCount + "");
            }

            if (chooseCount == 1) { //减号按钮不可点击
                subButton.setEnabled(false);
                addButton.setEnabled(true);
            }

        } else if (id == R.id.button_add) {
            if (mChooseCount.isFocused()) {
                chooseCount = chooseCountPre;
            }
            if (chooseCount < Math.min(mMaxValue, mAvaiValue)) {
                mChooseCount.clearFocus();
                //正常添加
                mChooseCount.setText(++chooseCount + "");
            } else if (mAvaiValue < mMaxValue) {
                //超出可用数量
            } else {
                //超过最大数量
            }
            if (chooseCount == Math.min(mMaxValue, mAvaiValue)) {
                subButton.setEnabled(true);   //加号按钮不可点击
                addButton.setEnabled(false);
            }
        }

    }


    public NumberButton setCurrentNumber(int currentNumber) {
        if (currentNumber < 1) mChooseCount.setText("");
        mChooseCount.setText("" + Math.min(Math.min(mMaxValue, mAvaiValue), currentNumber));
        return this;
    }


    public int getAvaiValue() {
        return mAvaiValue;
    }

    public int getCount() {
        return chooseCount;
    }

    public NumberButton setAvaiValue(int avaiValue) {
        mAvaiValue = avaiValue;
        return this;
    }

    public int getMaxValue() {
        return mMaxValue;
    }


    public NumberButton setMaxValue(int maxValue) {
        mMaxValue = maxValue;
        return this;
    }

    public NumberButton setOnCountChangeListener(OnCountChangeListener onCountChangeListener) {
        mOnCountChangeListener = onCountChangeListener;
        return this;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //当前数量
        if (!mChooseCount.getText().toString().trim().isEmpty()) {
            chooseCountPre = Integer.parseInt(mChooseCount.getText().toString().trim());
            int limit = Math.min(mMaxValue, mAvaiValue);

            if (chooseCountPre > limit) {

                mChooseCount.setText(limit + "");
                chooseCountPre = limit;
                mChooseCount.setSelection(mChooseCount.getText().toString().trim().length());
                subButton.setEnabled(true);   //加号按钮不可点击
                addButton.setEnabled(false);
                if (mAvaiValue < mMaxValue) {
                    //超过可用数量
                } else {
                    //超过最大数
                }
            } else if (chooseCountPre < 1) {
                mChooseCount.setText("1");
                chooseCountPre = 1;
                mChooseCount.setSelection(mChooseCount.getText().toString().trim().length());
                subButton.setEnabled(false);   //减号按钮不可点击
                addButton.setEnabled(true);
            } else if (chooseCountPre == limit) {
                subButton.setEnabled(true);   //加号按钮不可点击
                addButton.setEnabled(false);
            } else if (chooseCountPre == 1) {
                subButton.setEnabled(false);
                addButton.setEnabled(true);
            } else {
                subButton.setEnabled(true);   //减号按钮不可点击
                addButton.setEnabled(true);
            }
        }

    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (view.getId() == R.id.text_count) {
            if (!b) {
                if (mChooseCount.getText().toString().trim().isEmpty()) {
                    mChooseCount.setText(chooseCount + "");
                } else {
                    mChooseCount.setText(chooseCountPre + "");
                    chooseCount = chooseCountPre;
                }
                int limit = Math.min(mMaxValue, mAvaiValue);
                if (chooseCountPre == limit) {
                    subButton.setEnabled(true);   //加号按钮不可点击
                    addButton.setEnabled(false);
                } else if (chooseCountPre == 1) {
                    subButton.setEnabled(false);
                    addButton.setEnabled(true);
                } else {
                    subButton.setEnabled(true);   //减号按钮不可点击
                    addButton.setEnabled(true);
                }
            } else {
                mChooseCount.setText("");
                mChooseCount.requestFocus();
            }
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        mChooseCount.clearFocus();
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(mChooseCount.getWindowToken(), 0);
        }
        return true;
    }

    public interface OnCountChangeListener {
        void onCountChange(int count);
    }

}